package LeetCode.LeetCode2;

import java.util.Arrays;

/**
 * 重塑矩阵
 * @author lumin
 * @date 2022/03/24 14:27
 **/
public class Num566 {
    public static int[][] matrixReshape(int[][] mat, int r, int c) {
        if(mat.length * mat[0].length != r * c){
            return mat;
        }
        //新数组的行数
        int r1 = 0;
        ///新数组的列数
        int c1 = 0;
        int[][] arr = new int[r][c];
        //原数组的行数
        int m = 0;
        while(m < mat.length){
            //原数组的列数
            int n = 0;
            while(n < mat[0].length){
                arr[r1][c1] = mat[m][n];
                n++;
                if(c1 < c - 1){
                    c1++;
                }else{
                    c1 = 0;
                    r1++;
                }
            }
            m++;
        }
        return arr;
    }

    public static void main(String[] args) {
        int[][] mat = {{1},{2},{3},{4}};
        System.out.println(Arrays.toString(matrixReshape(mat,2,2)));
    }
}